home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / axheard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  3.3 KB  |  129 lines

  1. /*----------------------------------------------------------------------*
  2. * changed to include PID into link quality Packet                       *
  3. *-----------------------------------------------------------------------*/
  4.  
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "iface.h"
  8. #include "ax25.h"
  9. #include "ip.h"
  10. #include "timer.h"
  11.  
  12. static int MHwait = 0;          /* Semaphore to serialize access to heardlist*/
  13.  
  14. void
  15. init_maxheard(struct iface *ifp)
  16. {
  17.     ifp->Hmax = MAXDEFAULT;
  18.     ifp->Hcurrent = 0;
  19.     ifp->lq = cxallocw(MAXDEFAULT,sizeof(struct lq));
  20. }
  21.  
  22. int
  23. maxheard(struct iface *ifp,int num)
  24. {
  25. struct lq *lnew,*lold;
  26.  
  27.    /*-------------------------------------------------------------------*
  28.    * Don't blow the list away while somebody is writing a entry
  29.    *--------------------------------------------------------------------*/
  30.    if(num == 0)
  31.       num = 1;
  32.    lnew = cxallocw(num,sizeof(struct lq));
  33.    semwait(&MHwait,1);                  /* request/wait                 */
  34.    lold = ifp->lq;
  35.    ifp->lq = lnew;
  36.    ifp->Hcurrent = 0;
  37.    ifp->Hmax = num;
  38.    semrel(&MHwait);
  39.    xfree(lold);
  40.    return(0);
  41. }
  42.  
  43. static struct lq * near
  44. al_lookup(struct iface *ifp,char *addr)
  45. {
  46. register struct lq *lp;
  47. int i;
  48.  
  49.    semwait(&MHwait,0);
  50.  
  51.    lp = ifp->lq;
  52.    for(i = 0; i < ifp->Hmax; i++){
  53.       if(addreq(lp->addr,addr)){
  54.          return lp;
  55.       }
  56.       if (lp->time == 0)
  57.          break;
  58.       lp++;
  59.    }
  60.    return NULLLQ;
  61. }
  62.  
  63. /*----------------------------------------------------------------------*
  64. * Create a new entry in the AX.25 link table                            *
  65. *-----------------------------------------------------------------------*/
  66. static struct lq * near
  67. al_create(register struct iface *ifp,char *addr,char pid)
  68. {
  69. register struct lq *lp;
  70. register struct lq *lo = NULLLQ;
  71. int16 i;
  72. int32 time = currtime;
  73.  
  74.    semwait(&MHwait,1);
  75.  
  76.    if (ifp->Hcurrent < ifp->Hmax)   {
  77.       lp = &ifp->lq[ifp->Hcurrent];
  78.       ifp->Hcurrent++;
  79.    } else   {                           /* look for the oldest one      */
  80.       lp = ifp->lq;
  81.       for (i = 0; i < ifp->Hcurrent; i++)   {
  82.          if (lp->time < time)  {
  83.             time = lp->time;
  84.             lo = lp;
  85.          }
  86.          lp++;
  87.       }
  88.       lp = lo;
  89.    }
  90.    lp->pid = pid;
  91.    lp->currxcnt = 1;
  92.    memcpy(lp->addr,addr,AXALEN);
  93.    semrel(&MHwait);
  94.    return lp;
  95. }
  96.  
  97. /*----------------------------------------------------------------------*
  98. * Log the address of an incoming packet                                 *
  99. *-----------------------------------------------------------------------*/
  100. void
  101. logaddr(struct iface *ifp,char *addr,char pid)
  102. {
  103.    register struct lq *lp;
  104.  
  105.    if (MHwait)
  106.       return;
  107.    if(addreq(addr,ifp->hwaddr))
  108.       return;               /* Don't log our own packets if we hear them*/
  109.  
  110.    if((lp = al_lookup(ifp,addr)) == NULLLQ &&
  111.     (lp = al_create(ifp,addr,pid)) == NULLLQ)
  112.       return;
  113.    lp->pid = pid;
  114.    lp->currxcnt++;
  115.    lp->time = currtime;
  116. }
  117.  
  118. /*----------------------------------------------------------------------*
  119. *-----------------------------------------------------------------------*/
  120. void
  121. axflush(ifp)
  122. struct iface *ifp;
  123. {
  124.    semwait(&MHwait,1);
  125.    ifp->rawsndcnt = ifp->Hcurrent = 0;
  126.    memset(ifp->lq,0,ifp->Hmax * sizeof(struct lq));  /* brute force */
  127.    semrel(&MHwait);
  128. }
  129.